//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.ComponentModel;
using CommunityServer.Components;

namespace CommunityServer.Controls {

	public class StatusMessage : LiteralControl {
		bool success = true;
		bool isLicenseMessage = false;

		/// <summary>
		/// Gets or sets the name of the resource file to pull from
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "Gets or sets the name of the resource file." ),
		DefaultValue( null ),
		]
		public virtual String ResourceFile 
		{
			get 
			{
				Object state = ViewState["ResourceFile"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["ResourceFile"] = value;
			}
		}


		public StatusMessage() 
		{
			Visible = false;
		}

		public bool Success {
			get {
				return success;
			}
			set {
				success = value;
			}
		}

		public bool IsLicenseMessage 
		{
			get 
			{
				return isLicenseMessage;
			}
			set 
			{
				isLicenseMessage = value;
			}
		}

		/// <summary>
		/// Gets or sets the name of the resource to dispaly.
		
		/// </summary>
		[
		Bindable(true),
		Category( "Appearance" ),
		Description( "Gets or sets the name of the resource to dispaly." ),
		DefaultValue( "" ),
		]
		public virtual String ResourceName 
		{
			get 
			{
				Object state = ViewState["ResourceName"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "";
			}
			set 
			{
				ViewState["ResourceName"] = value;
			}
		}

		protected override void Render(HtmlTextWriter writer) 
		{

			if (!Visible)
				return;

			// What style to use
			if (!isLicenseMessage)
			{
				if (success)
					writer.AddAttribute(HtmlTextWriterAttribute.Class, "CommonMessageSuccess");
				else
					writer.AddAttribute(HtmlTextWriterAttribute.Class, "CommonMessageError");
			}
			else
			{
				if (success)
					writer.AddAttribute(HtmlTextWriterAttribute.Class, "CommonLicenseMessageSuccess");
				else
					writer.AddAttribute(HtmlTextWriterAttribute.Class, "CommonLicenseMessageError");
			}

			writer.RenderBeginTag(HtmlTextWriterTag.Div);

			writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
			writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
			writer.RenderBeginTag(HtmlTextWriterTag.Table);

			writer.RenderBeginTag(HtmlTextWriterTag.Tr);
			writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding-right: 8px;");
			writer.RenderBeginTag(HtmlTextWriterTag.Td);

			// What image to use
			if (success) {
				writer.AddAttribute(HtmlTextWriterAttribute.Src, Globals.GetSkinPath() + "/images/status-green.gif");
			} else {
				if (isLicenseMessage)
					writer.AddAttribute(HtmlTextWriterAttribute.Src, Globals.GetSkinPath() + "/images/status-yellow.gif");
				else
					writer.AddAttribute(HtmlTextWriterAttribute.Src, Globals.GetSkinPath() + "/images/status-red.gif");
			}

			writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
			writer.RenderBeginTag(HtmlTextWriterTag.Img);
			writer.RenderEndTag();

			writer.RenderEndTag();
			
			writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
			writer.RenderBeginTag(HtmlTextWriterTag.Td);

			if (Globals.IsNullorEmpty(this.ResourceName))
			{
				writer.Write(this.Text);
			}
			else
			{

		#if DEBUG
				try 
				{
		#endif
					writer.Write(  ResourceManager.GetString( ResourceName, ResourceFile ) );
		#if DEBUG			
				} 
				catch ( Exception ex ) 
				{
					writer.Write( "ResourceName: " + this.ResourceName + " Not Found" );
					System.Diagnostics.Debug.WriteLine( ex.ToString() );
				}
		#endif

			}

			writer.RenderEndTag();
			writer.RenderEndTag();
			writer.RenderEndTag();

			writer.RenderEndTag();
		}

	}
}
